# -*- coding: utf-8 -*-

import logging
import os
import re
import time
import datetime
import config

from scrapy.crawler import CrawlerProcess
from django.core.management.base import BaseCommand, CommandError
from scrapy.utils.log import configure_logging
from scrapy.utils.project import get_project_settings
from socar import settings
from sqlhelper import SqlHelper
from tools import decrypt_autohome_bbs
from tools.parse_autohome_bbs import parse_autohome_bbs
from tools.decrypt_bbs_phantomjs import DecryptBBSPhantomJs
from scrapy.selector import Selector


# 解密数据，解密之后重新插入数据库
# python manage.py decrypt_bbs_phantomjs
class Command(BaseCommand):
    help = 'decrypt storage bbs'

    def add_arguments(self, parser):
        parser.add_argument('-a', action = 'append', dest = 'spargs', default = [],
                            help = 'set spider argument (may be repeated)')

    # 必须实现的方法
    def handle(self, *args, **options):
        os.chdir(settings.BASE_DIR)
        spargs = arglist_to_dict(options['spargs']) or {}

        # 镜像数据库
        self.mirror_sql = SqlHelper()
        command = '''SELECT * FROM {table_name} WHERE `decrypt_status`=5''' \
            .format(table_name = config.bbs_mirror_temp_table_name)

        result = self.mirror_sql.query(command, cursor_type = 'dict')

        dbp = DecryptBBSPhantomJs()
        for res in result:
            print('id:%s  forum_id:%s' % (res.get('id'), res.get('forum_id')))

            try:
                conmain = res.get('conmain')
                sel = Selector(text = conmain)
                text = sel.xpath('//div[@class="w740"]').extract_first()
                text = dbp.decrypt_autohome_bbs(text, div = '.tz-paragraph')

                conmain = re.sub('<div class="tz-paragraph">(.*?)</div>', text, conmain)

                res['decrypt_status'] = 1
                res['conmain'] = conmain

                # 更新字段
                command = '''UPDATE {table_name} SET decrypt_status='{decrypt_status}', conmain='{conmain}' WHERE id={id}'''. \
                    format(table_name = config.bbs_mirror_temp_table_name, decrypt_status = res.get('decrypt_status'),
                           conmain = res.get('conmain').replace('\'', '\\\''), id = res.get('id'))

                self.mirror_sql.execute(command, commit = True)
            except Exception as e:
                print('decrypt_bbs_phantomjs exception e:%s' % str(e))

                # 当异常时，直接更改解密状态和存储状态
                command = '''UPDATE {table_name} SET decrypt_status='{decrypt_status}' WHERE id={id}'''. \
                    format(table_name = config.bbs_mirror_temp_table_name, decrypt_status = 1, id = res.get('id'))

                self.mirror_sql.execute(command, commit = True)


def arglist_to_dict(arglist):
    """Convert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a
    dict
    """
    return dict(x.split('=', 1) for x in arglist)
